﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Data;
namespace LicPdaService
{
    public class MainHandler
    {
        public static string lsReturn = "";
        public static string MessageHandler(string xml,string type)
        {
            XmlDocument doc = new XmlDocument();//实例化参数xml报文
            doc.LoadXml(xml);
            App.DBconSet(); //数据库连接设定

            //===========================================
            try
            {
                //登录
                if (type == "登陆")
                {
                    lsReturn = LicPdaService.Login.GetUser(xml);
                }
            }
            catch (Exception ex)
            {
                App.Log("登陆",ex.ToString());
                throw;
            }
            //===========================================
            try
            {
                if (type == "原料到货验收")
                {
                    lsReturn = LicPdaService.Check.GetCheck(xml);
                    
                }
            }
            catch (Exception ex)
            {
                App.Log("1.0原料到货验收", ex.ToString());
                throw;
            }
            //===========================================
            try
            {
                if (type == "原料上架")
                {
                    return LicPdaService.Putaway.GetPutaway(xml);
                }
            }
            catch (Exception ex)
            {
                App.Log("2.0原料上架", ex.ToString());
                throw;
            }
            //===========================================
            try
            {
                if (type == "原料上架打印")
                {
                    return LicPdaService.Putaway.Print(xml);
                }
            }
            catch (Exception ex)
            {
                App.Log("2.1原料上架打印", ex.ToString());
                throw;
            }

            //===========================================
            try
            {
                if (type == "ML库存")
                {
                    ////3.0通过ML获取库存-已测试完成,OK
                    lsReturn=LicPdaService.PDAXmlPrePickProcess.GetPDAPrePickData(doc);
                }
            }
            catch (Exception ex)
            {
                 App.Log("3.0获取库存", ex.ToString());
                throw;
            }
            //===========================================
            try
            {
                  if (type == "空箱扫描叫料")
                {
                    ////3.1空箱扫描叫料-已测试完成,OK
                    lsReturn=LicPdaService.PDAXmlOrderPickProcess.GetPDAOrderPickData(doc);
                }
            }
            catch (Exception ex)
            {
                 App.Log("3.1空箱扫描叫料", ex.ToString());
                throw;
            }

            //===========================================
            try
            {
                if (type == "Serial获取ASN")
                {
                    ////4.0通过Serial获取ASN明细-已测试完成,OK
                    lsReturn = LicPdaService.PDAXmlSerialProcess.GetPDASerialData(doc);
                }
            }
            catch (Exception ex)
            {
                App.Log("4.0Serial获取ASN", ex.ToString());
                throw;
            }
            //===========================================
            try
            {
                if (type == "成品上架库位")
                {
                    ////4.1成品上架库位-已测试完成,OK
                    lsReturn = LicPdaService.PDAXmlPutawayProcess.GetPDAPutawayData(doc);
                }
            }
            catch (Exception ex)
            {
                App.Log("4.1成品上架库位", ex.ToString());
                throw;
            }
            //===========================================
            try
            {
                if (type == "成品上架打印")
                {
                    ////4.2打印确认XML-已测试完成,OK
                    lsReturn = LicPdaService.PDAXmlPrintProcess.GetPDAPrintData(doc);
                }
            }
            catch (Exception ex)
            {
                App.Log("4.2成品上架打印", ex.ToString());
                throw;
            }

            //===========================================
            try
            {
                 if (type == "原料库存")
                {
                    ////5.0原料库存查询-已测试完成,OK
                   lsReturn= LicPdaService.PDAXmlInventoryProcess.GetPDAInventoryData(doc);
                }
            }
            catch (Exception ex)
            {
                App.Log("5.0原料库存查询", ex.ToString());
                throw;
            }
            //===========================================
            try
            {
                if (type == "成品库存")
                {
                    ////6.0成品库存查询-已测试完成,OK
                    lsReturn = LicPdaService.PDAXmlInproductProcess.GetPDAInproductData(doc);
                }

            }
            catch (Exception ex)
            {
                App.Log("6.0成品库存查询", ex.ToString());
                throw;
            }
            //===========================================

            try
            {
                 if (type == "ML库存明细")
                {
                    ////7.0通过ML获取库存明细-已测试完成,OK
                   lsReturn= LicPdaService.PDAXmlMLInLotProcess.GetPDAMLInLotData(doc);
                }

            }
            catch (Exception ex) {
                App.Log("7.0.库存转移-库存明细查询", ex.ToString());
            }


            //===========================================

            try
            {
                if (type == "库位转移")
                {
                    ////7.1通过ML获取库存明细-已测试完成,OK
                    lsReturn = LicPdaService.PDAXmlIntoMoveProcess.GetPDAIntoMoveData(doc);
                }

            }
            catch (Exception ex)
            {
                App.Log("7.1.库位转移-库位转移", ex.ToString());
            }


            //9.0通过Serial获取出货和库存信息 -测试完成
            //===========================================

            try
            {
                if (type == "SERIAL库存查询")
                {
                    //9.0通过Serial获取出货和库存信息
                    lsReturn = LicPdaService.PDAXmlSerialOrdersProcess.GetPDASerialOrdersData(doc);
                }

            }
            catch (Exception ex)
            {
                App.Log("9.0通过Serial获取出货和库存信息", ex.ToString());
            }


            ////9.1通过出货信息-分配库位
            ////===========================================

            //try
            //{
            //    if (type == "出货分配库位")
            //    {
            //        //9.1通过出货-分配库位
            //        lsReturn = LicPdaService.PDAXmlInventoryAllotProcess.GetPDAInventoryAllotData(doc);
            //    }

            //}
            //catch (Exception ex)
            //{
            //    App.Log("9.1通过出货信息-分配库位", ex.ToString());
            //}


            //9.2通过出货信息-分配库位--库位确认 - 测试完成
            //===========================================

            try
            {
                if (type == "出货拣货确认")
                {
                    //9.2通过出货信息-分配库位--库位确认
                    lsReturn = LicPdaService.PDAXmlInventoryAllotConfirmProcess.GetPDAInventoryAllotConfirmData(doc);
                }

            }
            catch (Exception ex)
            {
                App.Log("9.2通过出货信息-分配库位--库位确认", ex.ToString());
            }

            //99委托打印
            //===========================================
            try
            {
                if (type == "委托打印")
                {
                    lsReturn = LicPdaService.DelegatePrint.Print(xml);
                }
            }
            catch (Exception ex)
            {
                App.Log("99委托打印", ex.ToString());
                throw;
            }

            //10.0通过出货信息扫描生成托盘号--调拨通用方法
            //===========================================

            //try
            //{
            //    if (type == "拣货扫描")
            //    {
            //        //10.0通过出货信息-出库扫描--库位生成托盘号
            //        lsReturn = LicPdaService.PickUp.OrdGetPickUp(xml);

            //    }

            //}
            //catch (Exception ex)
            //{
            //    App.Log("10.0通过拣货信息-拣货扫描--返回扫描信息", ex.ToString());
            //}

            //try
            //{
            //    if (type == "打托")
            //    {
            //        //10.1通过出货信息-出库扫描--库位生成托盘号
            //        lsReturn = LicPdaService.PickUp.OrdTray(xml);

            //    }

            //}
            //catch (Exception ex)
            //{
            //    App.Log("10.1通过拣货信息-拣货扫描--生成托盘号", ex.ToString());
            //}

            //try
            //{
            //    if (type == "打印托盘标签")
            //    {
            //        //10.2通过出库单号、托盘号、打印托盘标签
            //        lsReturn = LicPdaService.PickUp.OrderPickTrayPrint(xml);

            //    }

            //}
            //catch (Exception ex)
            //{
            //    App.Log("10.2通过出库单号、托盘号、打印托盘标签", ex.ToString());
            //}

            //10.0通过出货信息扫描生成托盘号--调拨通用方法
            //===========================================
            try
            {
                if (type == "调拨通用方法")
                {
                    string type_temp = string.Empty;
                    DataTable ldt = LicPdaService.App.Xml2DataTable(xml);
                    if (ldt.Rows.Count == 0)
                    {
                        return "";

                    }

                    if (ldt.Rows.Count != 0)
                    {
                        type_temp = ldt.Rows[0]["MessageType"].ToString();
                    }

                    if (type_temp == "PickUp")
                    {
                        lsReturn = LicPdaService.PickUp.OrdGetPickUp(xml); //拣货查询
                    }
                    else if (type_temp == "PickUpTray")//生成托盘号
                    {
                        lsReturn = LicPdaService.PickUp.OrdTray(xml);
                    }
                    else if (type_temp == "PickUpPrint")//打印托盘号
                    {
                        lsReturn = LicPdaService.PickUp.OrderPickTrayPrint(xml);
                    }
                    //10.2通过出库单号、托盘号、打印托盘标签
                   // lsReturn = LicPdaService.PickUp.OrderPickTrayPrint(xml);

                }

            }
            catch (Exception ex)   
            {
                App.Log("10.0调拨入库通用方法、托盘号、打印托盘标签", ex.ToString());
            }

            //11.0根据调拨单--扫描调拨单、托盘号入库
            //===========================================
            try
            {
                if (type == "调拨入库通用方法")
                {
                    string type_temp = string.Empty;
                    DataTable ldt = LicPdaService.App.Xml2DataTable(xml);
                    if (ldt.Rows.Count == 0)
                    {
                        return "";

                    }

                    if (ldt.Rows.Count != 0)
                    {
                        type_temp = ldt.Rows[0]["MessageType"].ToString();//调拨入库类型
                    }

                    //if (type_temp == "AllocationKey")
                    //{
                    //    lsReturn = LicPdaService.PDAXmlAllocationReceipt.GetAllocationKey(xml);//判断扫描的调拨单是否存在系统中
                    //}
                    if (type_temp == "AllocationKey")
                    {
                        lsReturn = LicPdaService.PDAXmlAllocationReceipt.GetAllocationDetail(xml);//根据扫描的调拨单、托盘号带出的信息--入库
                    }

                    else if (type_temp == "GetAllocationLoc")
                    {
                        lsReturn = LicPdaService.PDAXmlAllocationReceipt.GetAllocationLoc(xml);//根据扫描的调拨单、选择库位
                    }
                    else if (type_temp == "AllocationReceipt")
                    {
                        lsReturn = LicPdaService.PDAXmlAllocationReceipt.AllocationReceipt(xml);//根据扫描的调拨单、入库（插入库存、库存属性表）
                    }
                  
                    //11.0通过扫描调拨单、入库

                }

            }
            catch (Exception ex)
            {
                App.Log("11.0根据调拨单--扫描调拨单、托盘号入库", ex.ToString());
            }

            return lsReturn;
        }
    }
}
